package com.juai.centralkitchen.activity;


import android.Manifest;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.bridge.common.base.BaseActivity;
import com.bridge.common.bean.Loginbean;
import com.bridge.common.bean.UserInfoBean;
import com.bridge.common.body.LoginBody;
import com.bridge.common.dialog.VDialog;
import com.bridge.common.http.RetrofitHelper;
import com.bridge.common.http.ToastUtil;
import com.bridge.common.network.HttpDisposable;
import com.bridge.common.utils.ActivityUtil;
import com.bridge.common.utils.SpUtils;
import com.juai.centralkitchen.R;
import com.juai.centralkitchen.databinding.ActivityMainBinding;
import com.juai.centralkitchen.jpushreceiver.TagAliasOperatorHelper;
import com.juai.centralkitchen.speechsynthesis.TtsSyntherizer;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ClassName SetttingActivity
 * @Description TODO 主页
 * @Author biekangdong
 * @CreateDate 2021/5/13 14:53
 * @Version 1.0
 * @UpdateDate 2021/5/13 14:53
 * @UpdateRemark 更新说明
 */
public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    ActivityMainBinding binding;

    String permissions[] = {
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.CHANGE_WIFI_STATE
    };

    private int isToMessage;

    @Override
    protected int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        binding = getBind();
        binding.titleView.setBackVisibility(false);
        andPermission();

        Log.e(TAG, "initView: " + JPushInterface.getRegistrationID(mContext));

        //判断是否是第一次启动app,并从通知栏跳转消息列表
        isToMessage = getIntent().getIntExtra("isToMessage", 0);
        if (isToMessage == 1) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(mContext, MessageActivity.class));

                }
            }, 200);
        }

        binding.tvKitchenName.setText(SpUtils.getString(mContext, SpUtils.KITCHENNAME, ""));
        httpGetCKManagerUserInfoByToken();
    }

    @Override
    protected void setListenner() {
        binding.llMessage.setOnClickListener(this);
        binding.llSetting.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.ll_message://消息
                startActivity(new Intent(this, MessageActivity.class));
                break;
            case R.id.ll_setting://设置
                startActivity(new Intent(this, SetttingActivity.class));
                break;
        }
    }

    public void andPermission() {
        AndPermission.with(mContext)
                .runtime()
                .permission(permissions)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> permissions) {
                        // 跳向主页

                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(@NonNull List<String> permissions) {
                    }
                })
                .start();
    }


    //获取厨房信息
    public void httpGetCKManagerUserInfoByToken() {
        RetrofitHelper.getApiService().getCKManagerUserInfoByToken()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpDisposable<UserInfoBean>() {
                    @Override
                    protected void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void success(UserInfoBean dataBean) {
                        if (dataBean.getCode() == 200) {
                            if (!TextUtils.isEmpty(dataBean.getData().getKitchen().getKitchenName())) {
                                SpUtils.putData(mContext, SpUtils.KITCHENNAME, dataBean.getData().getKitchen().getKitchenName());
                                binding.tvKitchenName.setText(dataBean.getData().getKitchen().getKitchenName());

                                //设置极光别名
                                TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
                                tagAliasBean.action = TagAliasOperatorHelper.ACTION_SET;
                                Set<String> stringsSet = new HashSet<>();
                                stringsSet.add(dataBean.getData().getKitchen().getClientId());
                                tagAliasBean.tags = stringsSet;
                                tagAliasBean.alias = dataBean.getData().getKitchen().getClientId();
                                TagAliasOperatorHelper.getInstance().handleAction(getApplicationContext(), 1, tagAliasBean);
                                //设置极光别名结束
                            }
                        }else if (dataBean.getCode() == 401){
                            ToastUtil.showToast(mContext, dataBean.getMsg());
                            SpUtils.putData(mContext,SpUtils.ISLOGIN,0);
                            SpUtils.putData(mContext,SpUtils.TOKEN,"");
                            ActivityUtil.finishAllActivity();
                            startActivity(new Intent(mContext,LoginActivity.class));
                        }else {
                            ToastUtil.showToast(mContext, dataBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        ToastUtil.showToast(mContext, msg);
                    }
                });

    }
}